package tech.dongling.boot.ioc;

import java.util.HashMap;
import java.util.Map;

public class LightIocContainer {
    private final Map<String, Object> beans = new HashMap<>();
    private final Map<Class<?>, String> typeToName = new HashMap<>();
    private final Map<String, Object> protocolHandlers = new HashMap<>();

    public void register(String name, Object bean) {
        beans.put(name, bean);
        typeToName.put(bean.getClass(), name);
    }

    public <T> T getBean(String name) {
        return (T) beans.get(name);
    }

    public <T> T getBean(Class<T> type) {
        String beanName = typeToName.get(type);
        return beanName == null ? null : (T) getBean(beanName);
    }

    public void registerProtocolHandler(String command, Object handler) {
        protocolHandlers.put(command, handler);
    }

    public Object getProtocolHandler(String command) {
        return protocolHandlers.get(command);
    }
}